package com.huishu.order.component;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.huishu.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName：RocketMqUpdateOrderReciever
 * @Author: ljj
 * @Date: 2023/4/13 17:37
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "async-order-topic", consumerGroup = "order-consumer-group")
public class RocketMqUpdateOrderReciever implements RocketMQListener<String> {

    @Autowired
    private OrderService orderService;

    @Override
    public void onMessage(String data) {
        Map<String, String> map = JSON.parseObject(data, Map.class);
        if (CollectionUtils.isEmpty(map)) {
            log.error("消费asyncOrderTopic参数为空");
            return;
        }
        // 修改订单状态和扣减库存
        orderService.updateStatusDeductionStock(map);
    }
}
